package com.ypinkj.app.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ypinkj.app.entity.*;
import com.ypinkj.app.mapper.*;
import com.ypinkj.app.utils.DateUtils;
import com.ypinkj.app.utils.resultutils.Result;
import com.ypinkj.app.utils.resultutils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.SoundbankResource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 *  个人信息
 * </p>
 *
 * @author LF
 * @since 2020-09-29
 */
@Api(tags = "简历个人信息")
@RestController
@RequestMapping("/u-resume-info")
public class UResumeInfoController {

    @Autowired
    UResumeJobhuntingMapper uResumeJobhuntingMapper;//求职意向
    @Autowired
    UResumeExperienceMapper resumeExperienceMapper;//项目经历
/*    @Autowired
    UResumeValuationMapper resumeValuationMapper;//自我评价*/
    @Autowired
    UResumeEducationMapper resumeEducationMapper;//教育背景
    @Autowired
    UResumeWorkMapper resumeWorkMapper;//工作经历
    @Autowired
    UResumeProgressMapper resumeProgressMapper;//个人特长
    @Autowired
    UResumeCertificateMapper resumeCertificateMapper;//技能证书
    @Autowired
    UResumeInfoMapper resumeInfoMapper;//个人信息

    @ApiOperation(value = "保存")
    @PutMapping
    public Result save(@RequestBody UResumeInfo resumeInfo){
        //查询毕业日期（最近一次的毕业时间）
        LambdaQueryWrapper<UResumeEducation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UResumeEducation::getUserid,resumeInfo.getUserid());
        wrapper.orderByDesc(UResumeEducation::getStartendtime);
        List<UResumeEducation> list = resumeEducationMapper.selectList(wrapper);
        UResumeEducation education = list.get(0);//倒叙按毕业时间取最新一条
        String[] strings = education.getStartendtime().split("~", -1);//截取字符串
        String s = strings[strings.length-1];//毕业日期
        String s2 = DateUtils.formatDate();//当前日期
        boolean b = DateUtils.precise(s, s2);//验证毕业时间是否大于当前日期
        if (b){
            String s1 = DateUtils.dayComparePrecise(s, s2);
            resumeInfo.setWorkyears(s1);
        }

        String id = resumeInfo.getId();
        //System.out.println(id);
        //如果保存的数据中存在id值则更新否则新增
        if (id==null){
            int i = resumeInfoMapper.insert(resumeInfo);
            return i>0 ? ResultUtil.success(resumeInfo) : ResultUtil.errorMsg();
        }else {
            int i = resumeInfoMapper.UpdateUResumeWorkById(resumeInfo);
            return i>0 ?ResultUtil.success() : ResultUtil.errorMsg();
        }
    }

    @ApiOperation(value = "列表")
    @GetMapping
    public Result getList(String userid){
        HashMap<Object, Object> map = new HashMap<>();//创建map集合传参
        //求职意向
        LambdaQueryWrapper<UResumeJobhunting> wrapper7 = new LambdaQueryWrapper<>();
        wrapper7.eq(UResumeJobhunting::getUserid,userid);
        List<UResumeJobhunting> list7 = uResumeJobhuntingMapper.selectList(wrapper7);
        map.put("UResumeJobhunting",list7.size()>0 ? list7 : null);
        //项目经验
        LambdaQueryWrapper<UResumeExperience> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(UResumeExperience::getUserid,userid);
        List<UResumeExperience> list1 = resumeExperienceMapper.selectList(wrapper1);
        map.put("UResumeExperience",list1.size()>0 ? list1 : null);
        //教育经历
        LambdaQueryWrapper<UResumeEducation> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(UResumeEducation::getUserid,userid);
        List<UResumeEducation> list2 = resumeEducationMapper.selectList(wrapper2);
        map.put("UResumeEducation",list2.size()>0 ? list2 : null);
        //查询当前用户的工作经历
        LambdaQueryWrapper<UResumeWork> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UResumeWork::getUserid,userid);
        List<UResumeWork> list = resumeWorkMapper.selectList(wrapper);
        map.put("UResumeWork",list.size()>0 ? list : null);
        //个人特长
        LambdaQueryWrapper<UResumeProgress> wrapper5 = new LambdaQueryWrapper<>();
        wrapper5.eq(UResumeProgress::getUserid,userid);
        List<UResumeProgress> list5 = resumeProgressMapper.selectList(wrapper5);
        map.put("UResumeProgress",list5.size()>0 ? list5 : null);
        //技能证书
        LambdaQueryWrapper<UResumeCertificate> wrapper4 = new LambdaQueryWrapper<>();
        wrapper4.eq(UResumeCertificate::getUserid,userid);
        List<UResumeCertificate> list4 = resumeCertificateMapper.selectList(wrapper4);
        map.put("UResumeCertificate",list4.size()>0 ? list4 : null);
        //个人信息
        List<UResumeInfo> list3 = resumeInfoMapper.getList(userid);
        map.put("UResumeInfo",list3.size()>0 ? list3 : null);

        return ResultUtil.success(map);
    }

    @ApiOperation(value = "根据id修改")
    @PostMapping
    public Result updateById(UResumeInfo resumeWork){
        int i = resumeInfoMapper.UpdateUResumeWorkById(resumeWork);
        return i>0 ?ResultUtil.success() : ResultUtil.errorMsg();
    }

    @ApiOperation(value = "根据userid获取个人信息")
    @GetMapping("/userInfo")
    public Result updateById(String userid){
        List<UResumeInfo> list3 = resumeInfoMapper.getList(userid);
        list3.forEach(System.out::println);
        return ResultUtil.success(list3.get(0));
    }
}
